<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button
              type="primary"
              @click="(addedFormVisible = true), (type = 0)"
              >新增司机</el-button
            >
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="60px"
          >
            <el-form-item label="司机" prop="name">
              <el-input
                v-model.trim="form.name"
                placeholder="司机姓名/手机"
                clearable
                class="wid220"
              ></el-input>
            </el-form-item>
            <el-form-item class>
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table" style="min-height: 670px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column
              label="名称"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="手机"
              prop="mobile"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="车辆" width="250" class-name="vehicle_hei">
              <template slot-scope="scope">
                <div
                  @click="id = scope.row.id"
                  class="vehicle"
                  @mouseover="scope.row.show = true"
                  @mouseleave="scope.row.show = false"
                  v-if="scope.row.status == 1"
                >
                  <div v-if="scope.row.car">
                    <div>{{ scope.row.car.plate_number }}</div>
                    <i
                      class="el-icon-close"
                      @click.stop="vehicle_de(scope.row.id)"
                      v-if="scope.row.show && scope.row.car"
                    ></i>
                  </div>
                  <el-select
                    v-model.trim="scope.row.car_id"
                    placeholder="车辆"
                    class="wid140"
                    @change="Vehicle(scope.$index)"
                    v-else
                  >
                    <el-option
                      v-for="(vehicle_list, index) in vehicle_list"
                      :label="vehicle_list.plate_number"
                      :value="vehicle_list.id"
                      :key="index"
                    ></el-option>
                  </el-select>
                </div>
                <div v-else>
                  {{ scope.row.car ? scope.row.car.plate_number : "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="状态"
              prop="name"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                <div class="main_link">
                  <el-link type="success" v-if="scope.row.status == 1"
                    >正常</el-link
                  >
                  <el-link type="danger" v-else>禁用</el-link>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="250"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="
                    (addedFormVisible = true),
                      (type = 1),
                      (id = scope.row.id),
                      edit_assig(scope.$index)
                  "
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 增加和编辑 -->
    <el-dialog
      :title="type == 1 ? '编辑司机' : '新增司机'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog500"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="名称：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="名称"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机：" prop="mobile">
          <el-input
            v-model.trim="info.mobile"
            placeholder="手机"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" :prop="type == 1 ? '' : 'password'">
          <el-input
            v-model.trim="info.password"
            show-password
            :placeholder="type == 1 ? '不填为默认密码' : '填写6位以上的密码'"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态：" prop="status" v-if="type == 1">
          <el-select
            v-model.trim="info.status"
            placeholder="状态"
            class="wid300"
          >
            <el-option label="禁用" :value="0"></el-option>
            <el-option label="正常" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  driver_index,
  driver_add,
  driver_edit,
  driver_delete,
  driver_getCar,
  driver_bindCar,
  driver_delCar,
  driver_confirmDelete,
} from "@/api/distribution/distributionDriver";
import rules from "@/rules/distribution/distributionDriver";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  components: {},
  data() {
    return {
      // 增加和编辑
      info: {
        name: "",
        mobile: "",
        password: "",
        status: 1,
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 车辆
      vehicle_list: [],
      // 搜索
      form: {
        vehicle: "",
      },
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        name: "",
        mobile: "",
        password: "",
        status: 1,
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 车辆
    vehicle_de(id) {
      let data = Object.assign(
        apiUrl().name,
        { id: id }
      );
      driver_delCar(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.driver_getCar();
          this.driver_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    Vehicle(index) {
      let data = Object.assign(
        apiUrl().name,
        {
          car_id: this.list.list.data[index].car_id,
          driver_id: this.list.list.data[index].id,
        }
      );
      driver_bindCar(data).then((res) => {
        if (res.errcode == 0) {
          this.$message({
            message: "绑定成功",
            type: "success",
          });
          this.driver_index();
          this.driver_getCar();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.driver_index();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.driver_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除司机?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          driver_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else if (res.errcode == 40030) {
              this.$confirm("是否强制删除司机?", "提示", {
                confirmButtonText: "强制删除",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  driver_confirmDelete(data).then((res) => {
                    if (res.errcode == 0) {
                      this.list.list.data.splice(index, 1);
                    }
                  });
                })
                .catch(() => {});
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (this.type == 1) {
            if (this.info.password != "" && this.info.password.length < 6) {
              this.$message.error("密码为6位以上");
              return;
            }
          }
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        name: this.list.list.data[index].name,
        password: "",
        mobile: this.list.list.data[index].mobile,
        status: this.list.list.data[index].status,
      };
    },
    // 新增保存
    add() {
      let data = Object.assign(
        apiUrl().name,
        this.info
      );
      driver_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.driver_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let data = Object.assign(
        apiUrl().name,
        this.info,
        { id: this.id }
      );

      driver_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.driver_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.driver_index();
    },
    changePage(page) {
      this.current.page = page;
      this.driver_index();
    },
    // 列表
    driver_index() {
      this.listLoading = true;
      let data = Object.assign(
        apiUrl().name,
        JSON.parse(JSON.stringify(this.form)),
        this.current
      );
      driver_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.car_id = e.car_id == 0 ? null : e.car_id;
            e.show = false;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 车辆列表
    driver_getCar() {
      let data = Object.assign(
        apiUrl().name
      );
      driver_getCar(data).then((res) => {
        if (res.errcode == 0) {
          this.vehicle_list = res.data.list;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.driver_index();
    this.driver_getCar();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 120px);
}
.main_table /deep/.el-table--small .vehicle_hei .cell {
  overflow: visible;
  height: auto;
  .vehicle {
    display: inline-block;
    position: relative;
    padding-right: 20px;
    i.el-icon-close {
      background-color: #c0c4cc;
      color: #909399;
      border-radius: 50%;
      text-align: center;
      cursor: pointer;
      font-size: 12px;
      height: 13px;
      width: 13px;
      line-height: 13px;
      vertical-align: middle;
      position: absolute;
      right: 0px;
      top: 4px;
      z-index: 9;
      &:hover {
        background-color: #e1e2e2;
      }
    }
    &:hover {
      cursor: pointer;
    }
  }
}
</style>
